package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesEquipmentRpcService;
import com.hunttown.mes.rpc.domain.MesEquipmentDTO;
import com.hunttown.mes.rpc.domain.query.MesEquipmentDTOQuery;
import com.hunttown.mes.service.MesEquipmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesEquipmentConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:25:13
 */
@Service
public class MesEquipmentRpcServiceImpl implements MesEquipmentRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesEquipmentRpcServiceImpl.class);

    private final MesEquipmentService mesEquipmentService;

    @Autowired
    public MesEquipmentRpcServiceImpl(MesEquipmentService mesEquipmentService) {
        this.mesEquipmentService = mesEquipmentService;
    }

    @Override
    public MesEquipmentDTO insert(MesEquipmentDTO mesEquipmentDTO) {
        try {
            return convert(mesEquipmentService.insert(convert(mesEquipmentDTO)));
        } catch (Exception e) {
            logger.error("MesEquipment create failed.", e);
            throw new RuntimeException("MesEquipment create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesEquipmentDTO mesEquipmentDTO) {
        try {
            return mesEquipmentService.updateInfoById(convert(mesEquipmentDTO));
        } catch (Exception e) {
            logger.error("MesEquipment update by id failed.", e);
            throw new RuntimeException("MesEquipment update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesEquipmentDTOQuery mesEquipmentDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesEquipmentDTOQuery.getW_id() != null && mesEquipmentDTOQuery.getW_id() > 0) {
                query.put("w_id", mesEquipmentDTOQuery.getW_id());
            }

            if (mesEquipmentDTOQuery.getIds() != null && !mesEquipmentDTOQuery.getIds().equals("")) {
                query.put("ids", mesEquipmentDTOQuery.getIds());
            }

            if (mesEquipmentDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesEquipmentDTOQuery.getStopFlag());
            }

            if (mesEquipmentDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentDTOQuery.getDeleteFlag());
            }

            return mesEquipmentService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesEquipment update by query failed.", e);
            throw new RuntimeException("MesEquipment update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentDTO selectById(Integer id) {
        try {
            return convert(mesEquipmentService.selectById(id));
        } catch (Exception e) {
            logger.error("MesEquipment select by id failed.", e);
            throw new RuntimeException("MesEquipment select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesEquipmentDTO selectObjByQuery(MesEquipmentDTOQuery mesEquipmentDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getEquipCode())) {
                query.put("equipCode", mesEquipmentDTOQuery.getEquipCode());
            }

            if (mesEquipmentDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesEquipmentDTOQuery.getStopFlag());
            }

            if (mesEquipmentDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesEquipmentDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getOrderBy())) {
                query.put("orderBy", mesEquipmentDTOQuery.getOrderBy());
            }

            return convert(mesEquipmentService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesEquipment select obj by query failed.", e);
            throw new RuntimeException("MesEquipment select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesEquipmentDTO> getListForPage(MesEquipmentDTOQuery mesEquipmentDTOQuery) {
        Query query = Query.page(mesEquipmentDTOQuery.getPn(), mesEquipmentDTOQuery.getPageSize());

        if (mesEquipmentDTOQuery.getId() != null && mesEquipmentDTOQuery.getId() > 0) {
            query.put("id", mesEquipmentDTOQuery.getId());
        }

        if (mesEquipmentDTOQuery.getIds() != null && !mesEquipmentDTOQuery.getIds().equals("")) {
            query.put("ids", mesEquipmentDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getLikeName())) {
            query.put("likeName", mesEquipmentDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getLikeCode())) {
            query.put("likeCode", mesEquipmentDTOQuery.getLikeCode());
        }

        if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getEquipCode())) {
            query.put("equipCode", mesEquipmentDTOQuery.getEquipCode());
        }

        if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getShowDate())) {
            query.put("showDate", mesEquipmentDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getStartDate())) {
            query.put("startDate", mesEquipmentDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getEndDate())) {
            query.put("endDate", mesEquipmentDTOQuery.getEndDate());
        }

        if (mesEquipmentDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesEquipmentDTOQuery.getStopFlag());
        }

        if (mesEquipmentDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesEquipmentDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesEquipmentDTOQuery.getOrderBy())) {
            query.put("orderBy", mesEquipmentDTOQuery.getOrderBy());
        }

        List<MesEquipmentDTO> list = convert(mesEquipmentService.selectByQuery(query));
        Page<MesEquipmentDTO> page = Page.from(mesEquipmentDTOQuery.getPn(), mesEquipmentDTOQuery.getPageSize(), list);
        page.setTotalItems(mesEquipmentService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesEquipmentService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesEquipment delete failed.", e);
            throw new RuntimeException("MesEquipment delete failed, Please order your code.", e);
        }
    }
}